using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using Bookvar.ServiceModel;

namespace Bookvar.UI.Adorners
{
    /// <summary>
    /// Simple helper class to allow any UIElements to be used as an Adorner.
    /// </summary>
    public class AdornerContainer : Adorner, IAdorner
    {
        private UIElement adorningElement;

        public AdornerContainer( UIElement adornedElement )
            : base( adornedElement )
        {
        }

        public AdornerContainer( UIElement adornedElement, UIElement adorningElement ) : base( adornedElement )
        {
            AdorningElement = adorningElement;
        }

        public UIElement AdorningElement
        {
            get { return adorningElement; }
            set
            {
                if ( value != null )
                {
                    AddVisualChild( value );
                    adorningElement = value;
                }
            }
        }

        protected override int VisualChildrenCount
        {
            get { return adorningElement == null ? 0 : 1; }
        }

        protected override Size ArrangeOverride( Size finalSize )
        {
            if ( adorningElement != null )
            {
                adorningElement.Arrange( new Rect( finalSize ) );
            }
            return finalSize;
        }

        protected override Visual GetVisualChild( int index )
        {
            if ( index == 0 && adorningElement != null )
            {
                return adorningElement;
            }
            return base.GetVisualChild( index );
        }

        bool IAdorner.IsVisible
        {
            get
            {
                return IsVisible;
            }
        }
    }
}